package com.mcflies.ui.aidl_service;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.mcflies.commons.McfliesConstant;
import com.mcflies.ui.R;

/**
 * @author:mcfly_jun
 * @create_date:2012-8-12
 * @comment:AIDL和远程Service--结合音乐播放器事例
 */
public class AIDLAndServicePlayActivity extends Activity implements
		OnClickListener {
	private ImageButton aidlServicePlay_IB_Favorite;
	private ImageButton aidlServicePlay_IB_Pre;
	private ImageButton aidlServicePlay_IB_Play;
	private ImageButton aidlServicePlay_IB_Next;
	private ImageButton aidlServicePlay_IB_Repeat;
	private SeekBar aidlServicePlay_SB;

	private IServicePlayer iPlayer;
	private boolean isPlaying = false;

	private ServiceConnection conn = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName componentname) {

		}

		@Override
		public void onServiceConnected(ComponentName componentname,
				IBinder service) {
			Log.i(McfliesConstant.MCFLIES_EXCEPTION_INFO_FLAG,
					"ServiceConnection-->onServiceDisconnected");
			iPlayer = IServicePlayer.Stub.asInterface(service);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aidl_service_play);
		initWidget();
		initService();

		aidlServicePlay_IB_Play.setOnClickListener(this);
		aidlServicePlay_SB
				.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
					@Override
					public void onStopTrackingTouch(SeekBar seekbar) {
					}

					@Override
					public void onStartTrackingTouch(SeekBar seekbar) {
					}

					@Override
					public void onProgressChanged(SeekBar seekbar, int i,
							boolean flag) {
						if (null != iPlayer) {
							try {
								iPlayer.seekTo(seekbar.getProgress());
							} catch (Exception e) {
								Log.i(McfliesConstant.MCFLIES_EXCEPTION_INFO_FLAG,
										e.getMessage());
							}
						}
					}
				});

		handler.post(updateThread);
	}

	private void initWidget() {
		aidlServicePlay_IB_Favorite = (ImageButton) findViewById(R.id.aidlServicePlay_IB_Favorite);
		aidlServicePlay_IB_Pre = (ImageButton) findViewById(R.id.aidlServicePlay_IB_Pre);
		aidlServicePlay_IB_Play = (ImageButton) findViewById(R.id.aidlServicePlay_IB_Play);
		aidlServicePlay_IB_Next = (ImageButton) findViewById(R.id.aidlServicePlay_IB_Next);
		aidlServicePlay_IB_Repeat = (ImageButton) findViewById(R.id.aidlServicePlay_IB_Repeat);
		aidlServicePlay_SB = (SeekBar) findViewById(R.id.aidlServicePlay_SB);
	}

	private void initService() {
		bindService(new Intent(AIDLAndServicePlayActivity.this,
				AIDLAndServiceMusicService.class), conn,
				Context.BIND_AUTO_CREATE);
		startService(new Intent(AIDLAndServicePlayActivity.this,
				AIDLAndServiceMusicService.class));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.aidlServicePlay_IB_Play:
			playOrPauseMusic();
			break;
		}
	}

	private void playOrPauseMusic() {
		if (!isPlaying) {
			try {
				iPlayer.play();
			} catch (Exception e) {
				Log.i(McfliesConstant.MCFLIES_EXCEPTION_INFO_FLAG,
						e.getMessage());
			}
			aidlServicePlay_IB_Play
					.setBackgroundResource(R.drawable.aidl_service_play_pause_button);
			isPlaying = true;
		} else {
			try {
				iPlayer.pause();
			} catch (Exception e) {
				Log.i(McfliesConstant.MCFLIES_EXCEPTION_INFO_FLAG,
						e.getMessage());
			}
			aidlServicePlay_IB_Play
					.setBackgroundResource(R.drawable.aidl_service_play_play_button);
			isPlaying = false;
		}
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
		};
	};

	private Runnable updateThread = new Runnable() {
		@Override
		public void run() {
			if (null != iPlayer) {
				try {
					aidlServicePlay_SB.setMax(iPlayer.getDuration());
					aidlServicePlay_SB
							.setProgress(iPlayer.getCurrentPosition());
				} catch (Exception e) {
					Log.i(McfliesConstant.MCFLIES_EXCEPTION_INFO_FLAG,
							e.getMessage());
				}
			}
			handler.post(updateThread);
		}
	};

}
